<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Mobile
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */
?>
<?php $_product = $this->getProduct() ?>
<?php $_id = $_product->getId(); ?>
<div class="giftcard-form">
    <script type="text/javascript">
         function parsePrice(value) {
            value = value.replace('\'', '').replace(' ', '');
            separatorComa = value.indexOf(',');
            separatorDot  = value.indexOf('.');

            if (separatorComa != -1 && separatorDot != -1) {
                if (separatorComa > separatorDot) {
                    value = value.replace('.', '').replace(',', '.');
                } else {
                    value = value.replace(',', '');
                }
            } else if (separatorComa != -1) {
                value = value.replace(',', '.');
            }

            return parseFloat(value);
        }
    </script>
<?php if ($_product->isSaleable() && $this->isConfigured($_product)): ?>
  <?php if (!$this->isOpenAmountAvailable($_product) && (count($this->getAmounts($_product)) == 1)): ?>

    <?php else: ?>
        <div class="fieldset giftcard-amount-form">
            <ul class="form-list">
                <li>
                    <?php if ($this->isAmountAvailable($_product)): ?>
                        <label for="giftcard_amount" id="amount_label_select" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount'); ?></label>
                    <?php endif; ?>
                    <?php if ($this->isOpenAmountAvailable($_product)): ?>
                        <label for="giftcard_amount_input" id="amount_label_input" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount in %s', $this->getCurrentCurrency()); ?></label>
                    <?php endif; ?>
                    <?php if ($this->isOpenAmountAvailable($_product)): ?>
                    <div id="giftcard_amount_box" class="">
                        <span class="v-fix"><input type="text" id="giftcard_amount_input" name="custom_giftcard_amount" class="validate-number required-entry input-text giftcard-amount-entry giftcard-min-amount giftcard-max-amount" /></span>

                        <?php if ($_product->getOpenAmountMin() || $_product->getOpenAmountMax()): ?>
                            <?php $_min = Mage::helper('core')->currency($_product->getOpenAmountMin(), true, false); ?>
                            <?php $_max = Mage::helper('core')->currency($_product->getOpenAmountMax(), true, false); ?>
                            <p class="giftcard-notice">
                                <?php if ($_product->getOpenAmountMax() && $_product->getOpenAmountMin()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                <?php elseif ($_product->getOpenAmountMin()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                <?php elseif ($_product->getOpenAmountMax()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                <?php endif; ?>
                            </p>                            
                            <script type="text/javascript">
                            //<![CDATA[
                                minAllowedAmount = <?php echo Mage::helper('core')->currency($_product->getOpenAmountMin(), false, false) ?>;
                                maxAllowedAmount = <?php echo Mage::helper('core')->currency($_product->getOpenAmountMax(), false, false) ?>;


                                Validation.add('giftcard-min-amount', '<?php echo $this->__('Entered amount is too low'); ?>', function(v) {
                                    return (parsePrice(v) >= minAllowedAmount);
                                });
                                Validation.add('giftcard-max-amount', '<?php echo $this->__('Entered amount is too high'); ?>', function(v) {
                                    if (maxAllowedAmount == 0) {return true;}
                                    return (parsePrice(v) <= maxAllowedAmount);
                                });
                            //]]>
                            </script>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    <div class="clear"></div>
                    <?php if ($this->isAmountAvailable($_product)): ?>
                    <div class="">
                        <select name="giftcard_amount" id="giftcard_amount" class="required-entry giftcard-amount-entry">
                            <option value=""><?php echo Mage::helper('enterprise_giftcard')->__('Choose an Amount...') ?></option>
                            <?php foreach ($this->getAmounts($_product) as $value): ?>
                                <option value="<?php echo $value; ?>"><?php echo Mage::helper('core')->currency($value, true, false); ?></option>
                            <?php endforeach; ?>

                            <?php if ($this->isOpenAmountAvailable($_product)): ?>
                                <option value="custom"><?php echo Mage::helper('enterprise_giftcard')->__('Other Amount...') ?></option>
                            <?php endif; ?>
                        </select>
                    </div>
                    <?php endif; ?>

                </li>
            </ul>
        </div>
        <script type="text/javascript">
        //<![CDATA[
        if ($('giftcard_amount') && $('giftcard_amount_input')) {
            $('giftcard_amount_input').hide();
            $('giftcard_amount_box').hide();
            $('amount_label_input').hide();
            Event.observe($('giftcard_amount'), 'change', switchGiftCardInputs);
            Event.observe($('giftcard_amount_input'), 'change', switchGiftCardInputs);
        }
        
        if (!$('giftcard_amount') && $('giftcard_amount_input')){
            Event.observe($('giftcard_amount_input'), 'change', switchGiftCardInput);
        }

        function updarePrice(priceValue){
            var format = <?php echo $this->helper('tax')->getPriceFormat(); ?>;
            var display = formatCurrency(priceValue, format);
            $j('span.price').html(display);
        }



        function switchGiftCardInput()
        {
            if ($('giftcard_amount') || !$('giftcard_amount_input')) {
                return;
            }
            value = $('giftcard_amount_input').value;

            newPrice = parsePrice($('giftcard_amount_input').value);
            if (newPrice != 'NaN'){
                updarePrice(newPrice);
            }

        }

        function switchGiftCardInputs()
        {
            if (!$('giftcard_amount') || !$('giftcard_amount_input')) {
                return;
            }            

            value = $('giftcard_amount').options[$('giftcard_amount').selectedIndex].value;
            if (value == 'custom') {
                $('giftcard_amount_input').show();
                $('giftcard_amount_box').show();

                $('amount_label_select').hide();
                $('amount_label_input').show();
            } else {
                $('giftcard_amount_input').hide();
                $('giftcard_amount_box').hide();

                $('amount_label_select').show();
                $('amount_label_input').hide();
            }

            var newPrice = 0;
            if (value == 'custom') {
                newPrice = parsePrice($('giftcard_amount_input').value);
            } else {
                newPrice = value;
            }

            if (newPrice != 'NaN'){
                updarePrice(newPrice);
            }
        }



        //]]>
        </script>
    <?php endif; ?>
    <div class="fieldset giftcard-send-form">
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <label for="giftcard_sender_name" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Sender Name'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_sender_name" name="giftcard_sender_name" class="required-entry input-text" value="<?php echo $this->getCustomerName(); ?>" />
                    </div>
                </div>
                <?php if ($this->isEmailAvailable($_product)): ?>
                <div class="field">
                    <label for="giftcard_sender_email" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Sender Email'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_sender_email" name="giftcard_sender_email" class="validate-email required-entry input-text" value="<?php echo $this->getCustomerEmail(); ?>"/>
                    </div>
                </div>
                <?php endif; ?>
            </li>
            <li class="fields">
                <div class="field">
                    <label for="giftcard_recipient_name" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Name'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_recipient_name" name="giftcard_recipient_name" class="required-entry input-text" />
                    </div>
                </div>
                <?php if ($this->isEmailAvailable($_product)): ?>
                <div class="field">
                    <label for="giftcard_recipient_email" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Email'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_recipient_email" name="giftcard_recipient_email" class="validate-email required-entry input-text" />
                    </div>
                </div>
                <?php endif; ?>
            </li>
        <?php if ($this->isMessageAvailable($_product)): ?>
            <li>
                <label for="giftcard_message"><?php echo Mage::helper('enterprise_giftcard')->__('Message'); ?></label>
                <div class="">
                    <textarea name="giftcard_message" id="giftcard_message" class="input-text giftcard-message-max-length" cols="5" rows="3"></textarea>
                    <?php if ($this->getMessageMaxLength()): ?>
                        <script type="text/javascript">
                            Validation.add('giftcard-message-max-length', '<?php echo $this->__('Maximum length of the message is %d characters.', $this->getMessageMaxLength()); ?>', function(v) {
                                return v.length <= <?php echo $this->getMessageMaxLength(); ?>;
                            });
                        </script>
                    <?php endif; ?>
                </div>
            </li>
        <?php endif; ?>
        </ul>
    </div>
<?php endif; ?>
</div>
